package com.yin.myproject.thread;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author yinxunzhi
 * Created on 2023/9/4
 */
public class ReentrantLockDemo01 {
    private static final ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
        new Thread(() -> {
            try {
                lock.lock();
                System.out.println("Thread 1: First lock acquired");

                // 在持有锁的情况下再次获取锁
                lock.lock();
                System.out.println("Thread 1: Second lock acquired");
            } finally {
                lock.unlock();
                System.out.println("Thread 1: First lock released");

                lock.unlock();
                System.out.println("Thread 1: Second lock released");
            }
        }).start();

        new Thread(() -> {
            try {
                lock.lock();
                System.out.println("Thread 2: First lock acquired");

                // 在持有锁的情况下再次获取锁
                lock.lock();
                System.out.println("Thread 2: Second lock acquired");
            } finally {
                lock.unlock();
                System.out.println("Thread 2: First lock released");

                lock.unlock();
                System.out.println("Thread 2: Second lock released");
            }
        }).start();
    }
}
